(function(){
    "use strict";
    app.controller('projectCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = ' 项目管理> 项目';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            var envId = sessionStorage.getItem('currentEnvId');
            //获取权限列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        projectName: $scope.searchByProjectName,
                        envId : envId
                    };
                httpLoad.loadData({
                    url:'/project/list',
                    method: 'POST',
                    data: JSON.stringify(params),
                    //noParam: true,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.projectList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            
            $scope.getData(1);
    //指定项目普通用户
    $scope.designateOrdinaryUser = function(item){  //打开模态
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/System/project/projectOrdinaryUser.html',  //指向上面创建的视图
            controller : 'projectOrdinaryModalCtrl',// 初始化模态范围
            resolve : {
                itemData: function() {
                    return item;
                }
            }
        });
        modalInstance.result.then(function(data){
        },function(){});
    };
    //指定项目管理员
    $scope.designateManagerUser = function(item){  //打开模态
        var modalInstance = $modal.open({
            templateUrl : '/statics/tpl/System/project/projectManager.html',  //指向上面创建的视图
            controller : 'projectManagerModalCtrl',// 初始化模态范围
            resolve : {
                itemData: function() {
                    return item;
                }
            }
        });
        modalInstance.result.then(function(data){
        },function(){});
    };
}
]);
  //指定项目管理员
    angular.module('app').controller('projectManagerModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
    	$scope.getData = function(page){
   		 $scope.param = {
   	                rows: 5
   	            };
   		 var projectId = itemData.projectId;
         var envId = itemData.envId;
     	  $scope.param.page = page || $scope.param.page;
           var params = {
                   page: $scope.param.page,
                   rows: $scope.param.rows,
                   projectId:projectId,
                   envId:envId
               }
   	httpLoad.loadData({
		          url:'/user/list',
		          method:'POST',
                 data: params,
		          success:function(data){
		        	  if(data.rows&&data.rows.length!=0){
                         $scope.userList = data.rows;
                         $scope.totalCount = data.records;
                         $scope.isImageData = false;
                         angular.forEach($scope.userList, function(data,index){
                             if(data.isLocked){
                                 data.isfreeze=true;
                                 data.isthaw=false;
                             }else{
                                 data.isfreeze=false;
                                 data.isthaw=true;
                             }
                         });
                     }else{
                         $scope.isImageData = true;
                     }
		          }
		      });
   	};
   		$scope.getData(1);
   		$scope.cid = {
   		};
           $scope.ok = function(){
       		var userId = $scope.cid.userId;
       		var projectId = itemData.projectId;
       		var envId = itemData.envId;
               httpLoad.loadData({
                   url:'/role/addProjManager',
                   method:'POST',
                   data:JSON.stringify({envId:envId,userId:userId,projectId:projectId}),
                   success:function(data){
                       if(data.success){
                           $scope.pop(data.message);
                           $modalInstance.close();
                       }else{
                           $scope.pop(data.message);
                           $modalInstance.close();
                       }
                   }
               });
           };
           $scope.cancel = function(){
               $modalInstance.dismiss('cancel'); // 退出
           }
       }]);
  //指定项目普通用户
    angular.module('app').controller('projectOrdinaryModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
    	$scope.getData = function(page){
   		 $scope.param = {
   	                rows: 5
   	            };
   		 var projectId = itemData.projectId;
         var envId = itemData.envId;
     	  $scope.param.page = page || $scope.param.page;
           var params = {
                   page: $scope.param.page,
                   rows: $scope.param.rows,
                   projectId:projectId,
                   envId:envId,
                   roleId:4
               }
   	httpLoad.loadData({
		          url:'/user/getSelectUser',
		          method:'POST',
                 data: params,
		          success:function(data){
		        	  if(data.rows&&data.rows.length!=0){
                         $scope.userList = data.rows;
                         $scope.selectUser = data.data;
                         $scope.totalCount = data.records;
                         $scope.isImageData = false;
                         $scope.selectUsers = data.data;
                         $scope.isSelected = function(id){          
                             return $scope.selectUsers.indexOf(id)!=-1; 
                             //将result与checkbox的当前id即value值匹配
                             //有就返回的true，没有就返回false
                         };
                         angular.forEach($scope.userList, function(data,index){
                        	 /*for(var i=0;i<selectUsers.length;i++){
                        		 if(data.userId = selectUser[i].userId)
                        			 $scope.row.checked = true;
                        	 }*/
                             if(data.isLocked){
                                 data.isfreeze=true;
                                 data.isthaw=false;
                             }else{
                                 data.isfreeze=false;
                                 data.isthaw=true;
                             }
                         });
                     }else{
                         $scope.isImageData = true;
                     }
		          }
		      });
   	};
   		$scope.getData(1);
   		
   		$scope.cid = {
   		};
           $scope.ok = function(){
        	   //获取选中的userId
        	   var envId = itemData.envId;
        	   var projectId = itemData.projectId;
        	   var chekedIds=[];
        	   angular.forEach($scope.userList, function(data){
	        		 if(data.checked){
	        			 var javaObject = {};
	        			 javaObject.userId = data.userId;
	        			 javaObject.envId = envId;
	        			 javaObject.projectId = projectId;
	        			 //角色为普通用户4
	        			 javaObject.roleId = 4;
	        			 chekedIds.push(javaObject);
	        		 }
        		});
               httpLoad.loadData({
                   url:'/role/addOrdinaryUser',
                   method:'POST',
                   data:JSON.stringify(chekedIds),
                   success:function(data){
                       if(data.success){
                           $scope.pop(data.message);
                           $modalInstance.close();
                       }else{
                           $scope.pop(data.message);
                           $modalInstance.close();
                       }
                   }
               });
           };
           $scope.cancel = function(){
               $modalInstance.dismiss('cancel'); // 退出
           }
       }]);
})();